package com.yqg.education.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yqg.education.common.Result;
import com.yqg.education.common.interceptor.SysLog;
import com.yqg.education.entity.UserRole;
import com.yqg.education.service.IUserRoleService;
import com.yqg.education.utils.Constants;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * UserRoleController 前端控制器
 * @author zxf
 * @since 2025-03-01
 */
@RestController
@RequestMapping("/user-role")
public class UserRoleController {

    @Resource
    private IUserRoleService userRoleService;

    /**
    *  新增
    */
    @PostMapping("/edit")
    public Result save(@RequestBody UserRole userRole) {
        return Result.success(userRoleService.saveOrUpdate(userRole));
    }
    /**
     *  修改
     */
    @PostMapping("/updateUserRole")
//    @SysLog("修改用户角色")
    @Transactional(rollbackFor = Exception.class)
    public Result updateUserRole(@RequestBody List<Long> checkedId){
        if (!userRoleService.updateUserRole(checkedId)) {
            return Result.error(Constants.CODE_500, "修改用户角色失败");
        }
        return Result.success();
    }
    /**
    *  删除
    */
    @GetMapping("delete/{id}")
    public Result delete(@PathVariable Integer id) {
        UserRole userRole =userRoleService.getById(id);
        return Result.success(userRoleService.saveOrUpdate(userRole));
    }
    /**
    *  查询所有
    */
    @GetMapping("/all")
    public Result findAll() {
        QueryWrapper<UserRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete", "0");
       List<UserRole> list=  userRoleService.list(queryWrapper);
        return Result.success(list);
    }
    /**
    *  根据id查询
    */
    @GetMapping("/{id}")
    public Result findOne(@PathVariable Integer id) {
        return Result.success(userRoleService.getById(id));
    }
    /**
    *  分页查询
    */
    @GetMapping("/page")
    public Result findPage(@RequestParam Integer pageNum,
                                @RequestParam Integer pageSize) {
        QueryWrapper<UserRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete", "0");
        queryWrapper.orderByDesc("id");
        return Result.success(userRoleService.page(new Page<>(pageNum, pageSize), queryWrapper));
    }

}

